// 渲染用户信息模块
function renderAuthor(currentUser) {
    document.querySelector('.author-avatar').src = currentUser.avatar
    document.querySelector('.author-username').textContent = currentUser.username
    document.querySelector('.author-git').herf = currentUser.gitRepo
}

// 渲染文章数量，分类数量
function renderCount(articleCount, typeCount) {
    document.querySelector('.article-count').textContent = articleCount
    document.querySelector('.type-count').textContent = typeCount
}

// 渲染内容模块
function renderArticleList(articleList) {
    var container = document.querySelector('.articleList')
    for (var i in articleList) {
        var article = articleList[i]
        var html = `<div class="blog">` +
                `<div class="title">${article.title}</div>` +
                `<div class="date">${article.publishedAt}</div>` +
                `<div class="desc">${article.content}</div>` +
                `<a href="blog_content.html?aid=${article.aid}" class="detail">查看全文 &gt;&gt;</a>` +
            `</div>`;
        container.innerHTML += html
    }
}

var xhr = new XMLHttpRequest();
// xhr.open('get', '/json/article_list.json')
xhr.open('get', '/article_json')
xhr.onload = function () {
    alert(this.responseText)
    var data = JSON.parse(this.responseText)
    if (!data.currentUser) {
        location.assign('/login.html')
        return
    }
    renderAuthor(data.currentUser)
    renderCount(data.articleCount, data.typeCount)
    renderArticleList(data.articleList)
}

xhr.send()
